/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nio;

import java.awt.EventQueue;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;

/**

 @author Tomek
 */
public class FileServer extends javax.swing.JFrame {
	Serwer server = new Serwer();
	DefaultListModel listModel;
	/**
	 Creates new form FileServer
	 */
	public FileServer() {
		server.start();
		initComponents();
		listModel = new DefaultListModel();
		fileList.setModel(listModel);
	}
	private void serveFile(final File file){
		server.pliki.put(file.getName(), file);
		EventQueue.invokeLater(new Thread(){
			@Override
			public void run() {
				listModel.addElement(file.getName());
			}
		});
	}
	private void removeFile(final String fileName){
		server.pliki.remove(fileName);
		EventQueue.invokeLater(new Thread(){
			@Override
			public void run() {
				listModel.removeElement(fileName);
			}
		});
	}
	/**
	 This method is called from within the constructor to initialize the form.
	 WARNING: Do NOT modify this code. The content of this method is always
	 regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        fileList = new javax.swing.JList();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        fileList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(fileList);

        addButton.setText("Dodaj");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Usuń");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addButton)
                    .addComponent(removeButton))
                .addGap(0, 10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(removeButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
		for(Object o : fileList.getSelectedValuesList()){
			removeFile((String)o);
		}
	}//GEN-LAST:event_removeButtonActionPerformed

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
		int option = fileChooser.showDialog(this, "Choose File");
		if(option == JFileChooser.APPROVE_OPTION){
			serveFile(fileChooser.getSelectedFile());
		}
	}//GEN-LAST:event_addButtonActionPerformed

	/**
	 @param args the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new FileServer().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JList fileList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables
}
